<template>
  <div>
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="searchList"
          size="small"
          label-width="140px"
        >
          <el-form-item label="订单号:">
            <el-input
              style="width: 203px"
              v-model="searchList.orderId"
              placeholder="请输入要搜索的订单号"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="订单状态:">
            <el-select
              v-model="searchList.userOrderStatus"
              style="width: 203px"
              placeholder="请选择订单状态"
              clearable
            >
              <el-option
                v-for="(item, index) in userOrderStatusOptions"
                :key="index"
                :value="item.value"
                :label="item.label"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <div></div>
    <el-card style="margin-top: 15px" class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div style="margin-top: 15px" class="table-container">
      <el-table
        :data="orderList"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="订单号" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.orderId }}</template>
        </el-table-column>
        <el-table-column label="地址" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.address }}</template>
        </el-table-column>
        <!-- <el-table-column label="公司名称" width="150" align="center">
            <template slot-scope="scope">{{ scope.row.companyName }}</template>
          </el-table-column> -->
        <el-table-column label="需求" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.description }}</template>
        </el-table-column>
        <!-- <el-table-column label="更新时间" width="160" align="center">
            <template slot-scope="scope">{{ scope.row.updateTime }}</template>
          </el-table-column> -->
        <!-- <el-table-column label="服务ID" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.serverId }}</template>
        </el-table-column> -->
        <!-- <el-table-column label="顾客ID" width="150" align="center">
            <template slot-scope="scope">{{ scope.row.userId }}</template>
          </el-table-column> -->
        <el-table-column label="顾客名" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.customerName }}</template>
        </el-table-column>
        <!-- <el-table-column label="公司ID" width="150" align="center">
            <template slot-scope="scope">{{ scope.row.companyId }}</template>
          </el-table-column> -->
        <!-- <el-table-column label="需求ID" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.demandId }}</template>
        </el-table-column> -->
        <!-- <el-table-column label="创建时间" width="160" align="center">
            <template slot-scope="scope">{{ scope.row.createTime }}</template>
          </el-table-column> -->
        <el-table-column label="联系电话" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.phone }}</template>
        </el-table-column>
        <el-table-column label="金额" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.price }}</template>
        </el-table-column>
        <el-table-column label="数量" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.qty }}</template>
        </el-table-column>
        <el-table-column label="开始服务日期" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.startTime }}</template>
        </el-table-column>
        <el-table-column label="订单状态" width="150" align="center">
          <template slot-scope="scope">
            <el-tag
              type="info"
              effect="light"
              v-if="scope.row.userOrderStatus == 'TODO'"
              >待完成</el-tag
            >
            <el-tag
              type=""
              effect="plain"
              v-else-if="scope.row.userOrderStatus == 'DOING'"
              >进行中</el-tag
            >
            <el-tag
              type="success"
              effect="plain"
              v-else-if="scope.row.userOrderStatus == 'DONE'"
              >已完成</el-tag
            >
            <!-- {{ scope.row.userOrderStatus }} -->
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="300" align="center">
          <template slot-scope="scope">
            <!-- 状态修改按钮 -->
            <el-button
              size="mini"
              type="primary"
              @click="
                (statusEditDialogVisible = true),
                  showStatusData(scope.$index, scope.row)
              "
              >状态修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 订单状态编辑弹窗 -->
    <el-dialog
      title="订单状态编辑"
      :visible.sync="statusEditDialogVisible"
      width="35%"
    >
      <span>
        <el-card class="filter-container" shadow="never">
          <div style="margin-top: 15px">
            <el-form
              ref="editStatus"
              :inline="true"
              :model="editStatus"
              size="small"
              label-width="140px"
            >
              <el-form-item label="订单状态:">
                <el-select
                  v-model="editStatus.userOrderStatus"
                  style="width: 203px"
                  placeholder="请选择订单状态"
                  clearable
                >
                  <!-- <el-option label="" value=""></el-option> -->
                  <!-- <el-option label="待完成" value="TODO"></el-option>
                  <el-option label="进行中" value="DOING"></el-option>
                  <el-option label="已完成" value="DONE"></el-option> -->
                  <el-option
                    v-for="(item, index) in userOrderStatusOptions"
                    :key="index"
                    :value="item.value"
                    :label="item.label"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-form>
          </div>
        </el-card>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="statusEditDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEditStatus()">确 定</el-button>
      </span>
    </el-dialog>
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        layout="prev, pager, next"
        :page-count="pageCunt"
        :current-page.sync="currentPage"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "managementOrder",
  data() {
    return {
      dialogVisible: false,
      pageCunt: 1,
      // messageDialogVisible: false,

      searchList: {
        orderId: "",
        userOrderStatus: "",
        pageNum: "1",
        pageSize: "5",
      },
      currentPage: 1,
      currentOrderList: {
        pageNum: "1",
        pageSize: "5",
      },
      editStatus: {
        orderId: "",
        userOrderStatus: "",
      },
      orderList: [
        {
          address: "",
          commentStatus: "",
          companyId: "",
          createTime: "",
          customerName: "",
          description: "",
          email: "",
          orderId: "",
          phone: "",
          price: "",
          qty: "",
          serverId: "",
          startTime: "",
          status: "",
          updateTime: "",
          userId: "",
          userOrderStatus: "",
        },
      ],

      userOrderStatusOptions: [
        { type: "", value: "TODO", label: "待完成" },
        { type: "", value: "DOING", label: "进行中" },
        { type: "", value: "DONE", label: "已完成" },
      ],
      listLoading: true,
      statusEditDialogVisible: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      // 发送请求获取订单列表
      // ...
      this.listLoading = true;
      this.axios
        .get("/api/company/auth/orderCompanyUser/list", {
          params: this.currentOrderList,
        })
        .then((response) => {
          console.log(response);
          if (response.data.code == "200") {
            this.orderList = response.data.value.data;

            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleSearchList() {
      this.listLoading = true;
      this.axios
        .get("/api/company/auth/orderCompanyUser/list", {
          params: this.searchList,
        })
        .then((response) => {
          // console.log(response);
          if (response.data.code == "200") {
            this.orderList = response.data.value.data;

            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleResetSearch() {
      this.searchList = {
        orderId: "",
        userOrderStatus: "",
        pageNum: "1",
        pageSize: "5",
      };
    },
    // 换页
    handleCurrentChange(val) {
      this.listLoading = true;
      this.currentOrderList.pageNum = val;
      this.axios
        .get("/api/company/auth/orderCompanyUser/list", {
          params: this.currentOrderList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.orderList = response.data.value.data;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    // getOrderStatusText(status) {
    //   switch (status) {
    //     case "TODO":
    //       return "待完成";
    //     case "DOING":
    //       return "进行中";
    //     case "DONE":
    //       return "已完成";
    //   }
    // },
    // 关闭
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    // 状态编辑
    handleEditStatus() {
      this.$confirm("请再次确认是否选择修改").then(() => {
        this.axios
          .post(
            "/api/company/auth/orderCompanyUser/update?orderId=" +
              this.editStatus.orderId +
              "&userOrderStatus=" +
              this.editStatus.userOrderStatus
          )
          .then((response) => {
            if (response.data.code == "200") {
              this.$message({
                message: "修改成功",
                type: "success",
              });
              this.getList();
              this.statusEditDialogVisible = false;
            } else {
              this.$message.error(response.data.message);
            }
          })
          .catch((error) => {
            this.$message.error("修改失败");
          });
      });
    },
    // 状态
    showStatusData(index) {
      this.editStatus = { ...this.orderList[index] };
    },
  },
};
</script>
